# -*- cod1ng:UTF-8 -*- #
"""
@filename:008加并发症.py
@author:Qian
@time:2025-01-04
"""
from neo4j import GraphDatabase
import pandas as pd

class AnorexiaKnowledgeGraph:
    def __init__(self, uri, user, password):
        self.driver = GraphDatabase.driver(uri, auth=(user, password))

    def close(self):
        self.driver.close()

    def create_nodes_and_relationships(self, drugs=None):
        # 定义疾病及其相关信息
        diseases_info = {
            "厌食症": {
                "symptoms": [],
                "drugs":[],
                "risk_factors": [],
                "treatments": [],
                "research_studies": [
                    "学术论文",
                    "临床研究",
                    "发表论文",
                    "发表论文",
                    "学术期刊",
                    "学术会议"

                ],
                "experts": [],
                "support_organizations": [
                    "非营利组织",
                    "支持小组",

                    "医疗组织",
                    "卫生部门",
                    "公共卫生机构",
                    "社会福利组织",
                    "政策制定机构",
                    "科研机构",
                    "非营利组织",
                    "医疗保健组织",
                    "医疗服务机构",
                    "医疗卫生机构",
                    "医疗器械制造商"

                ]
            },
            "抑郁症": {
                "drugs":[],
                "symptoms": [],
                "risk_factors": [],
                "treatments": [],
                "research_studies": [
                    "流行病学研究",
                    "临床试验",
                    "学术论文",
                    "发表论文",
                    "学术期刊",
                    "学术会议"
                ],
                "experts": [],
                "support_organizations": [
                    "心理健康协会",
                    "支持小组",
                    "非营利组织",
                    "医疗组织",
                    "卫生部门",
                    "公共卫生机构",
                    "社会福利组织",
                    "政策制定机构",
                    "科研机构",
                    "非营利组织",
                    "医疗保健组织",
                    "医疗服务机构",
                    "医疗卫生机构",
                    "医疗器械制造商"
                ]
            },
            "焦虑症": {
                "symptoms": [],
                "risk_factors": [],
                "treatments": [],
                "research_studies": [
                    "焦虑症流行病学研究",
                    "临床治疗研究",
                    "学术论文",
                    "发表论文",
                    "学术期刊",
                    "学术会议"

                ],
                "experts": [],
                "support_organizations": [
                    "焦虑症协会",
                    "支持小组"
                ],
                "drugs": [
                ]
            },
            # 其他疾病信息...
        }


        csv_file = 'data0/experts.csv'
        risk_factors_df = pd.read_csv(csv_file)
        experts = risk_factors_df['专家'].tolist()
        diseases_info["厌食症"]["experts"] = experts

        csv_file = 'data0/experts2.csv'
        risk_factors_df = pd.read_csv(csv_file)
        experts2 = risk_factors_df['专家'].tolist()
        diseases_info["抑郁症"]["experts"] = experts2

        csv_file = 'data0/experts3.csv'
        # 读取 CSV 文件
        risk_factors_df = pd.read_csv(csv_file)
        # 将药物信息转换为列表
        experts3 = risk_factors_df['专家'].tolist()
        # 将新的 "drugs" 节点添加到焦虑症的信息中
        diseases_info["焦虑症"]["experts"] = experts3

        csv_file = 'data0/treatments.csv'
        risk_factors_df = pd.read_csv(csv_file)
        treatments = risk_factors_df['治疗方法'].tolist()
        # 将新的 "drugs" 节点添加到厌食症的信息中
        diseases_info["厌食症"]["treatments"] = treatments

        csv_file = 'data0/treatments2.csv'
        # 读取 CSV 文件
        risk_factors_df = pd.read_csv(csv_file)
        # 将药物信息转换为列表
        treatments2 = risk_factors_df['治疗方法'].tolist()
        # 将新的 "drugs" 节点添加到抑郁症的信息中
        diseases_info["抑郁症"]["treatments"] = treatments2

        csv_file = 'data0/treatments3.csv'
        # 读取 CSV 文件
        risk_factors_df = pd.read_csv(csv_file)
        # 将药物信息转换为列表
        treatments3 = risk_factors_df['治疗方法'].tolist()
        # 将新的 "drugs" 节点添加到焦虑症的信息中
        diseases_info["焦虑症"]["treatments"] = treatments3



        csv_file = 'data0/risk_factors.csv'
        risk_factors_df = pd.read_csv(csv_file)
        risk_factors = risk_factors_df['风险因素'].tolist()
        # 将新的 "drugs" 节点添加到厌食症的信息中
        diseases_info["厌食症"]["risk_factors"] = risk_factors

        csv_file = 'data0/risk_factors2.csv'
        # 读取 CSV 文件
        risk_factors_df = pd.read_csv(csv_file)
        # 将药物信息转换为列表
        risk_factors2 = risk_factors_df['风险因素'].tolist()
        # 将新的 "drugs" 节点添加到厌食症的信息中
        diseases_info["抑郁症"]["risk_factors"] = risk_factors2

        csv_file = 'data0/risk_factors3.csv'
        # 读取 CSV 文件
        risk_factors_df = pd.read_csv(csv_file)
        # 将药物信息转换为列表
        risk_factors3 = risk_factors_df['风险因素'].tolist()
        # 将新的 "drugs" 节点添加到厌食症的信息中
        diseases_info["焦虑症"]["risk_factors"] = risk_factors3

        csv_file = 'data0/medications.csv'
        # 读取 CSV 文件
        drugs_df = pd.read_csv(csv_file)
        # 将药物信息转换为列表
        anxiety_drugs = drugs_df['药物名称'].tolist()
        # 将新的 "drugs" 节点添加到厌食症的信息中
        diseases_info["抑郁症"]["drugs"] = anxiety_drugs

        csv_file = 'data0/medications2.csv'
        # 读取 CSV 文件
        drugs_df = pd.read_csv(csv_file)
        # 将药物信息转换为列表
        drugs2 = drugs_df['药物名称'].tolist()
        # 将新的 "drugs" 节点添加到厌食症的信息中
        diseases_info["厌食症"]["drugs"] = drugs2

        csv_file = 'data0/medications3.csv'
        # 读取 CSV 文件
        drugs_df = pd.read_csv(csv_file)
        # 将药物信息转换为列表
        drugs3 = drugs_df['药物名称'].tolist()
        # 将新的 "drugs" 节点添加到厌食症的信息中
        diseases_info["焦虑症"]["drugs"] = drugs3

        csv_file = 'data0/symptoms.csv'
        # 读取 CSV 文件
        symptoms_df = pd.read_csv(csv_file)
        # 将药物信息转换为列表
        symptoms = symptoms_df['症状'].tolist()
        # 将新的 "drugs" 节点添加到厌食症的信息中
        diseases_info["焦虑症"]["symptoms"] = symptoms

        csv_file = 'data0/symptoms2.csv'
        # 读取 CSV 文件
        symptoms_df = pd.read_csv(csv_file)
        # 将药物信息转换为列表
        symptoms2 = symptoms_df['症状'].tolist()
        # 将新的 "drugs" 节点添加到厌食症的信息中
        diseases_info["厌食症"]["symptoms"] = symptoms2

        csv_file = 'data0/symptoms3.csv'
        # 读取 CSV 文件
        symptoms_df = pd.read_csv(csv_file)
        # 将药物信息转换为列表
        symptoms3 = symptoms_df['症状'].tolist()
        # 将新的 "drugs" 节点添加到厌食症的信息中
        diseases_info["抑郁症"]["symptoms"] = symptoms3

        complications_info = {
            "身体并发症": {
                "营养不良": "由于食物摄入不足，导致体内重要营养素缺乏。",
                "心血管问题": "低血压、心律不齐、心脏肥大，严重时可能导致心脏衰竭。",
                "骨骼问题": "骨密度降低，增加骨折风险，可能导致骨质疏松。",
                "消化系统问题": "胃肠功能紊乱，包括便秘、腹胀、胃食管反流等。",
                "内分泌问题": "生理周期不规律或停经，影响生育能力。",
                "电解质失衡": "可能导致严重的身体功能障碍，包括心脏和肌肉功能障碍。",
                "肾脏问题": "脱水和电解质失衡可能导致肾功能受损。",
                "肝脏问题": "高血压、肝硬化、肝癌等。",
                "肾上腺问题": "高血脂、甲状腺疾病等。",
                "胆囊炎": "胆囊炎可能导致胆囊功能障碍。",
                "甲状腺问题": "甲状腺功能障碍、甲状腺癌等。",
                "乳腺问题": "乳腺癌、乳腺功能障碍等。",
                "结直肠癌": "结直肠癌可能导致骨质疏松、高血压、肝硬化等。",
                "骨髓瘤": "骨髓瘤可能导致骨质疏松、高血压、肝硬化等。",
                "其他": "其他并发症，如高血糖、糖尿病、肥胖、癌症等。"
            },
            "心理并发症": {
                "抑郁症": "患者常伴有抑郁症状，影响情绪和日常生活。",
                "焦虑障碍": "增加社交焦虑、强迫症状或广泛性焦虑。",
                "自杀风险": "由于严重的心理痛苦，患者自杀风险显著增加。",
                "人格障碍": "一些患者可能发展出特定的人格障碍（如边缘型人格障碍）。",
                "精神分裂症": "患者可能因精神分裂而出现分裂症状。",
                "创伤后应激障碍": "患者因创伤而出现应激障碍。",
                "其他": "其他心理并发症，如精神分裂症、创伤后应激障碍等。"

            },
            "其他问题": {
                "社交隔离": "患者可能因饮食障碍而疏远家人和朋友，导致孤独感。",
                "学业和职业影响": "由于身体和心理健康问题，影响学习和工作表现。",
                "长期健康风险": "一些并发症可能导致长期健康问题，影响生活质量。",
                "其他": "其他问题，如生活质量、收入等。"
            }
        }

        with self.driver.session() as session:
            # 创建所有疾病及其相关节点和关系
            for disease, info in diseases_info.items():
                # 创建疾病节点
                session.run("CREATE (:Disease {name: $name})", name=disease)

                # 创建症状节点
                for symptom in info["symptoms"]:
                    session.run("MERGE (:Symptom {name: $name})", name=symptom)
                    session.run("""
                        MATCH (d:Disease {name: $disease}), (s:Symptom {name: $name})
                        CREATE (d)-[:表现为]->(s)
                    """, disease=disease, name=symptom)

                # 创建风险因素节点
                for risk_factor in info["risk_factors"]:
                    session.run("MERGE (:RiskFactor {name: $name})", name=risk_factor)
                    session.run("""
                        MATCH (d:Disease {name: $disease}), (r:RiskFactor {name: $name})
                        CREATE (r)-[:增加风险]->(d)
                    """, disease=disease, name=risk_factor)

                # 创建治疗方法节点
                for treatment in info["treatments"]:
                    session.run("MERGE (:Treatment {name: $name})", name=treatment)
                    session.run("""
                        MATCH (d:Disease {name: $disease}), (t:Treatment {name: $name})
                        CREATE (t)-[:治疗]->(d)
                    """, disease=disease, name=treatment)

                # 创建相关研究节点
                for research in info["research_studies"]:
                    session.run("MERGE (:Research {name: $name})", name=research)
                    session.run("""
                        MATCH (d:Disease {name: $disease}), (r:Research {name: $name})
                        CREATE (r)-[:研究]->(d)
                    """, disease=disease, name=research)

                # 创建专家节点
                for expert in info["experts"]:
                    session.run("MERGE (:Expert {name: $name})", name=expert)
                    for treatment in info["treatments"]:
                        session.run("""
                            MATCH (e:Expert {name: $name}), (t:Treatment {name: $treatment})
                            CREATE (e)-[:提供]->(t)
                        """, name=expert, treatment=treatment)

                # 创建支持组织节点
                for organization in info["support_organizations"]:
                    session.run("MERGE (:SupportOrganization {name: $name})", name=organization)
                    session.run("""
                        MATCH (d:Disease {name: $disease}), (o:SupportOrganization {name: $name})
                        CREATE (o)-[:提供支持]->(d)
                    """, disease=disease, name=organization)

                # 创建药物节点
                if "drugs" in info and info["drugs"]:  # 检查药物信息是否存在且不为空
                    for drug in info["drugs"]:
                        # 使用 MERGE 确保药物节点唯一
                        session.run("MERGE (:Drug {name: $name})", name=drug)
                        # 使用 MATCH 来创建关系
                        session.run("""
                            MATCH (d:Disease {name: $disease}), (drug:Drug {name: $name})
                            CREATE (d)-[:用药于]->(drug)
                        """, disease=disease, name=drug)

            # 创建并发症及其节点和关系
            for category, complications in complications_info.items():
                for complication, description in complications.items():
                    session.run("CREATE (:Complication {name: $name, description: $description})", name=complication, description=description)
                    for disease in diseases_info.keys():
                        session.run("""
                            MATCH (c:Complication {name: $name}), (d:Disease {name: $disease})
                            CREATE (d)-[:可能导致]->(c)
                        """, name=complication, disease=disease)

if __name__ == "__main__":
    # 替换为您的 Neo4j 连接信息
    uri = "bolt://localhost:7687"
    user = "neo4j"
    password = "Dhdr8818341"

    graph = AnorexiaKnowledgeGraph(uri, user, password)
    graph.create_nodes_and_relationships()
    graph.close()